var MongodbConnect=require('./MongodbConnect.js')
let mongodb_connect=new MongodbConnect("mongodb://localhost:27017/")
var objectId = require('mongodb').ObjectId;
class Manage{
	constructor(){
        this.dbo;
    }
    async openDatabase(name){
        this.dbo=await mongodb_connect.open(name)
    }
	login(user_name,password){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").find({"user_name":user_name}).toArray((err, result)=>{ // 返回集合中所有数据
                if (err) reject(err);
                if(result.length==0){
                    resolve({"status":-1});
                }
                else{
                    if(result[0].password==password){
                        resolve({"status":2,"result":result[0]});
                    }else{
                        resolve({"status":0});
                    }
                }
            });  
		}) 
	}
    loginBigUser(user_name,password){
        return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").find({"user_name":user_name}).toArray((err, result)=>{ // 返回集合中所有数据
                if (err) reject(err);
                if(result.length==0){
                    resolve({"status":-1});
                }
                else{
                    if(result[0].password==password){
                        resolve({"status":2,"result":result[0]});
                    }else{
                        resolve({"status":0});
                    }
                }
            });  
		})       
    }
    getUserInfoById(user_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").findOne({"_id":objectId(user_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
         }) 
    }
    addExcel(author_id,author_name,excel_name,content,date){
        var excel = {
            "excel_name": excel_name,
            "content": content,
            "author_name":author_name,
            "author_id": author_id,
            "date":date
        }
		return new Promise((resolve, reject) => {
            this.dbo.collection("excel").insertOne(excel, (err, res)=>{
                if (err) reject(err);
                console.log("文档插入成功");
                resolve({"status":2,"result":res});
            });

		}) 
    }
    addPdf(pdf_name,user_id,user_name,pdfbase64,date){
        var pdf = {
            "pdf_name": pdf_name,
            "user_id": user_id,
            "user_name":user_name,
            "pdfbase64": pdfbase64,
            "date":date
        }
		return new Promise((resolve, reject) => {
            this.dbo.collection("pdf").insertOne(pdf, (err, res)=>{
                if (err) reject(err);
                console.log("pdf插入成功");
                resolve({"status":2,"result":res});
            });

		}) 
    }
    getPdfByUserId(user_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("pdf").find({"user_id":user_id}).toArray( (err, res)=>{
                if (err) reject(err);
                resolve(res);
            });
		}) 
    }
    getExcelListByAuthorId(author_id){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").findOne({"_id":objectId(author_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                let list=res.excel_list;
                resolve(list);
            });  
		}) 
    }
    addExcelSummaryToExcelList(author_id,excel_list,excel_id,excel_name,date,author_name){
        let excel_summary={
            "excel_id":excel_id.toString(),
            "excel_name":excel_name,
            "date":date,
            "author_id":author_id,
            "author_name":author_name
        }
        console.log(excel_summary)
        excel_list.push(excel_summary)
        let update_obj={
            $set:{ "excel_list":excel_list}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(author_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });

		}) 
    }
    updateExcelSummaryToExcelList(author_id,excel_id,excel_name,excel_list){
        for(let i=0;i<excel_list.length;i++){
            if(excel_list[i].excel_id==excel_id){
                excel_list[i].excel_name=excel_name
                break
            }
        }
        let update_obj={
            $set:{ "excel_list":excel_list}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(author_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档列表更新成功");
                resolve({"status":2});
            });

		})        
    }
	getExcelListByUserId(user_id){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").findOne({"_id":objectId(user_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                let list=res.excel_list;
                resolve(list);
            });  
		}) 		
	}
    getExcelByExcelId(excel_id){
		return new Promise((resolve, reject) => {
            this.dbo.collection("excel").findOne({"_id":objectId(excel_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
		}) 
    }
    getUsers(){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").find({}).toArray( (err, res)=>{
                if (err) reject(err);
                resolve(res);
            }); 
		})         
    }
    getBigUsers(){
        return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").find({}).toArray( (err, res)=>{
                if (err) reject(err);
                resolve(res);
            }); 
		})         
    }
    addUser(user){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").insertOne(user, (err, res)=>{
                if (err) reject(err);
                console.log("user插入成功");
                resolve({"status":2,"result":res});
            });

		})         
    }
    addBigUser(user){
		return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").insertOne(user, (err, res)=>{
                if (err) reject(err);
                console.log("biguser插入成功");
                resolve({"status":2,"result":res});
            });

		})         
    }
    userLink(user_id,superior_id,superior_name){
        let update_obj={
            $set:{ "superior":{
                "superior_id":superior_id,
                "superior_name":superior_name
            }
        }
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(user_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
		})       
    }
    bigUserLink(user_id,link_info){
        let update_obj={
            $set:{ "link_department":link_info}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").updateOne({"_id":objectId(user_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
		})        
    }
    getOldSuperiorSubDepartment(old_superior_id){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").findOne({"_id":objectId(old_superior_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                let list=res.sub_department;
                resolve(list);
            });  
		}) 	
    }
    updateOldSuperior(sub_department,old_superior_id,user_id){
        let list=[]
        for(let i=0; i<sub_department.length;i++){
            if(sub_department[i].user_id!=user_id){
                list.push(sub_department[i])
            }
        }
        let update_obj={
            $set:{ "sub_department":list}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(old_superior_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
		}) 
    }
    getNewSuperiorSubDepartment(superior_id){
		return new Promise((resolve, reject) => {
            this.dbo.collection("user").findOne({"_id":objectId(superior_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                let list=res.sub_department;
                resolve(list);
            });  
		}) 
    }
    updateNewSuperior(sub_department,superior_id,user_id, department_name){
        let list=sub_department
        let tmp={
            "user_id":user_id,
            "department_name":department_name
        }
        list.push(tmp)
        let update_obj={
            $set:{ "sub_department":list}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(superior_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
            })
    }
    deleteExcelByExcelId(excel_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("excel").deleteOne({"_id":objectId(excel_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
		})        
    }
    deletePdfByPdfId(_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("pdf").deleteOne({"_id":objectId(_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
		})           
    }
    deleteUserById(_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").deleteOne({"_id":objectId(_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
		})           
    }
    deleteBigUserById(_id){
        return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").deleteOne({"_id":objectId(_id)}, (err, res)=>{ // 返回集合中所有数据
                if (err) reject(err);
                resolve(res);
            });  
		})          
    }
    updatePassword(user_id,password){
        let update_obj={
            $set:{ "password":password}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(user_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
		})         
    }
    updateBigPassword(user_id,password){
        let update_obj={
            $set:{ "password":password}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("big_user").updateOne({"_id":objectId(user_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });
		})        
    }
    deleteExcelFromUserList(excel_id,user_id,excel_list){
        let res_list=[]
        for(let i=0; i<excel_list.length; i++){
            if(excel_list[i].excel_id!=excel_id){
                res_list.push(excel_list[i])
            }
        }
        let update_obj={
            $set:{ "excel_list":res_list}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("user").updateOne({"_id":objectId(user_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("更新成功");
                resolve({"status":2});
            });

		})
    }
    updateExcel(excel_id,content,excel_name){
        let update_obj={
            $set:{ "content":content,"excel_name":excel_name}
        }
        return new Promise((resolve, reject) => {
            this.dbo.collection("excel").updateOne({"_id":objectId(excel_id)}, update_obj,  (err, res)=>{
                if (err) reject(err);
                console.log("文档更新成功");
                resolve({"status":2});
            });

		}) 
    }
}

module.exports = Manage;